/***********************************************************************
	Copyright (C) 2011 Bertrand GUILLAUMOND
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class PmManager{
	
	private Vector<Message> messages = new Vector<Message>();
	private PmManagerFrame frame;

	public PmManager()
	{
		frame = new PmManagerFrame();
	}

	public void parseXml()
	{
		String author;
		String subject;
		String content;
	
		try {
			  File file = new File("messages.xml");
			  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			  DocumentBuilder db = dbf.newDocumentBuilder();
			  Document doc = db.parse(file);
			  doc.getDocumentElement().normalize();
			  NodeList nodeLst = doc.getElementsByTagName("pm");
			  System.out.println("Parsing all messages ...");

			  for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);
		
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
			  
					Element fstElmnt = (Element) fstNode;
					NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("author");
					Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
					NodeList fstNm = fstNmElmnt.getChildNodes();
					//System.out.println("Author : "  + ((Node) fstNm.item(0)).getNodeValue());

					author = ((Node) fstNm.item(0)).getNodeValue();
					  
					NodeList midNmElmntLst = fstElmnt.getElementsByTagName("subject");
					Element midNmElmnt = (Element) midNmElmntLst.item(0);
					NodeList midNm = midNmElmnt.getChildNodes();
					//System.out.println("Subject : " + ((Node) midNm.item(0)).getNodeValue());
					
					subject = ((Node) midNm.item(0)).getNodeValue();

					NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("content");
					Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
					NodeList lstNm = lstNmElmnt.getChildNodes();
					//System.out.println("Content : " + ((Node) lstNm.item(0)).getNodeValue());

					content = ((Node) lstNm.item(0)).getNodeValue();

					messages.add(new Message(author, subject, content));
				}

		  	}
		  } 
		catch (Exception e)
		{
			e.printStackTrace();
		}

		System.out.println(messages.size()+" messages parsed from XLM extract");
	}

	public void printMessages()
	{
		System.out.println("Printing "+messages.size()+" messages ...");
		for(int i = 0; i< messages.size(); i++)
        {
			frame.addMessage(messages.get(i));
		}
		frame.setVisible(true);
	}

	private class PmManagerFrame extends JFrame
	{
		private JPanel topPanel;
        private JPanel midPanel;
        private JPanel lowPanel;
        private JButton loginButton;
 		private JTextField usernameText;
		private JPasswordField passwordText;
        
       	public PmManagerFrame()
       	{
		    setTitle("Messages privés");
		    setLayout(new GridLayout(3,0,10,10));
		    topPanel = new JPanel();
		    midPanel = new JPanel();
		    lowPanel = new JPanel();

		    topPanel.setLayout(new FlowLayout());
		    midPanel.setLayout(new FlowLayout());
		    lowPanel.setLayout(new FlowLayout());
		                   
		    getContentPane().add(topPanel);
		    getContentPane().add(midPanel);
		    getContentPane().add(lowPanel);

		    addButtons();
		    addEvents();

		    setSize(500,200);
  		}

		

        public void addButtons()
        {

                        
        }

        public void addEvents()
       	{
  
        }

        public void actionPerformed(ActionEvent ev)
        {       

        }

		public void addMessage(Message m)
		{
			System.out.println("Adding message from "+m.getAuthor());
			midPanel.add(new JLabel("Message"));
			midPanel.add(m.getPanel());
			getContentPane().repaint();
		}

	}

}
